package dataObjects
{
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;

	public class User
	{
		public var username:String;
		public var albums: ArrayCollection;
		public var cards: ArrayCollection;
		public var wishlist: ArrayCollection;
		public var tradelist: ArrayCollection;
				
		public function User(username:String,
							 albums: ArrayCollection,
							 cards: ArrayCollection,
							 wishlist: ArrayCollection,
							 tradelist: ArrayCollection)
		{
			this.username = username;
			this.albums = albums;
			this.cards = cards;
			this.wishlist = wishlist;
			this.tradelist = tradelist;
			
		}
		
		public function getAlbum(albumID:int):AlbumInstance{
			for each (var a:AlbumInstance in albums){
				if (a.templateID==albumID)
					return a;
			}
			return null;
		}
		
		public function getUnpastedCards(albumID:int):ArrayCollection{
			var album:AlbumInstance = getAlbum(albumID);
			var ans:ArrayCollection = new ArrayCollection();
			
			for each (var card:CardInstance in this.cards){
				if (!isPasted(card,album))
					ans.addItem(card);
			}
			return ans;
		}
		
		
		public function getUnpastedCardsForAlbum(albumID:int):ArrayCollection{
			var album:AlbumInstance = getAlbum(albumID);
			var ans:ArrayCollection = new ArrayCollection();
			
			for each (var card:CardInstance in this.cards){
				if(card.albumId == albumID){
					if (!isPasted(card,album))
						ans.addItem(card);
				}
			}
			return ans;
		}
		
		public function getPastedCards(albumID:int):ArrayCollection{
			var album:AlbumInstance = getAlbum(albumID);
			var ans:ArrayCollection = new ArrayCollection();
			
			for each (var card:CardInstance in this.cards){
				if (isPasted(card,album))
					ans.addItem(card);
			}
			return ans;
		}
		
		public function isPasted(card:CardInstance,album:AlbumInstance):Boolean{
			for each(var cardInstanceID:int in album.pasted){
				if (cardInstanceID==card.instanceID) return true;
			}
			return false;		
		}
		
		public function isTemplatePasted(card:CardTemplate):Boolean{
			for each(var id:int in listOfPastedTemplatesIDs()){
				if (id==card.templateID) return true;
			}
			return false;		
		}
		
		public function listOfPastedTemplatesIDs():ArrayCollection{
			var ans:ArrayCollection = new ArrayCollection();
			
			for each (var cardI:CardInstance in cards)
				for each (var albumI:AlbumInstance in albums)
					if (isPasted(cardI,albumI))
						ans.addItem(cardI.templateID);
					
			return ans;
		}
		
		public function intersectWishTradeLists(wishList:ArrayCollection,tradeList:ArrayCollection):ArrayCollection{
			var ans:ArrayCollection = new ArrayCollection();
			for each (var wishCard:CardTemplate in wishList){
				for each (var tradeCard:CardTemplate in tradeList){
					if (wishCard.templateID == tradeCard.templateID)
						ans.addItem(wishCard);
				}//inner for
			}//outer for
			
			return ans;
		}
		
		
		
		public function getNumeOfcardsInWishlist():int
		{
			var ans:int = 0;
			for each (var wishCard:CardTemplate in wishlist)
				ans++;
			return ans;
		}
		
		public function getNumeOfcardsInTradelist():int
		{
			var ans:int = 0;
			for each (var tradeCard:CardTemplate in tradelist)
				ans++;
			return ans;
		}
		
		public function getCardWithID(instanceID:int):CardTemplate
		{
			var pastedCard:CardTemplate;
			for each (var card:CardInstance in cards)
			{
				if (card.instanceID == instanceID)
					pastedCard = card.asTemplate();
			}	
			return pastedCard;
		}
		
		public function getIndexOfCard(templateID:int):int
		{
			var selectedIndex:int = 0;
			for each (var card:CardTemplate in tradelist)
			{
				if (card.templateID == templateID)
					return selectedIndex;
				else
					selectedIndex++;
			}
			if (selectedIndex >= getNumeOfcardsInWishlist())
				return -1;
			return selectedIndex;
		}
		
	}
}